package com.asset.salesforce.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.Login;

@Repository
public class LoginDaoImpl {
	@Autowired
	private SessionFactory sessionFactory;
	
	public void addLogin(Login login) {
		try{
		sessionFactory.getCurrentSession().save(login);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Login> getAllLogin() {
		
		return sessionFactory.getCurrentSession().createQuery("from login").list();
		
		
	}
 public void saveOrUpdateLogin(Login login) {
		
	 Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(login);
		
		
	}
	@SuppressWarnings("unchecked")
	public Login getLogin(String email , String password) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Login where  email = :email and password = :password");
		query.setParameter("email", email);
		query.setParameter("password", password);
		List<Login>  logins= query.list();
		if(logins != null && logins.size() > 0)
		return logins.get(0);
		else
			return new Login();
		
		
	}
	@SuppressWarnings("unchecked")
	public Login getLoginById(Integer empId) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Login where empId=:empId");
		query.setParameter("empId", empId);
		List<Login>  logins= query.list();
		if(logins != null && logins.size() > 0)
		return logins.get(0);
		else
			return new Login();		
	}
	public void deleteLogin(Integer id) {
		Login login = (Login) sessionFactory.getCurrentSession().load(
				Login.class, id);
		if (null != login) {
			sessionFactory.getCurrentSession().delete(login);
		}
		
	}

}
